package com.diankangwo.util;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import java.io.*;

/**
 * Created by wuye on 2017/11/14.
 */
public class textDemo {

    public static void main(String[] args) {
//        GeneraeHtml generaeHtml=new GeneraeHtml();
//        try
//        {
            File file=new File("/Users/wuye/Desktop/第二卷/山有木兮木有枝/1.doc");
//            generaeHtml.generateHtmlByFile(file);
//        }catch(Exception e)
//        {
//            e.printStackTrace();
//        }

        String str = readToString("asd");
        System.out.println(str);
    }

    public static String readToString(String fileName){

//        File file = new File(fileName);
//        File file=new File("/Users/wuye/Desktop/第二卷山有木兮木有枝/1.doc");
//        File uFile = new File("/Users/wuye/Desktop/第二卷山有木兮木有枝/1.doc");
        /*
         word 操作
         */
        String path = "/Users/wuye/Desktop/第二卷山有木兮木有枝/1.doc";
        String s = "";
        try {
            if(path.endsWith(".doc")) {
                InputStream is = new FileInputStream(new File(path));
                WordExtractor ex = new WordExtractor(is);
                s = ex.getText();
            }else if (path.endsWith("docx")) {
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                s = extractor.getText();
            }else {
                System.out.println("传入的word文件不正确:"+path);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuffer bf = new StringBuffer(s);
        return bf.toString();



        /*
        ---------------------------------------------------------------------------------------------
         */
        /**
         * text操作
         */
//        File file=new File("/Users/wuye/Downloads/《星辰变》全本文字精校版.txt");
//        String encoding = "GBK";
//        Long filelength = file.length();
//        byte[] filecontent = new byte[filelength.intValue()];
//        try {
//            FileInputStream in = new FileInputStream(file);
//            in.read(filecontent);
//            in.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            return new String(filecontent, encoding);
//        } catch (UnsupportedEncodingException e) {
//            System.err.println("The OS does not support " + encoding);
//            e.printStackTrace();
//            return null;
//        }
    }

}
